package cn.darkhorse.alarm.service.impl;

import cn.darkhorse.alarm.dto.AlarmSettingPonitTablesDTO;
import cn.darkhorse.alarm.dto.PointTableParamsDTO;
import cn.darkhorse.alarm.mapper.ParameterSettingsMapper;
import cn.darkhorse.alarm.pojo.AlarmParameterSettings;
import cn.darkhorse.alarm.service.AlarmParameterSettingsService;
import cn.darkhorse.device.mapper.AlarmParameterSettingsMapper;
import cn.darkhorse.device.pojo.IotPointTable;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.IotPointTableService;
import cn.darkhorse.device.service.MesDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2023-04-07
 */
@Service
public class AlarmParameterSettingsServiceImpl extends ServiceImpl<ParameterSettingsMapper, AlarmParameterSettings> implements AlarmParameterSettingsService {

    @Resource
    private IotPointTableService iotPointTableService;

    @Resource
    private ParameterSettingsMapper parameterSettingsMapper;

    @Resource
    private MesDeviceService mesDeviceService;


    /**
     * @param wrapper
     * @return
     */
    @Override
    public Map<Long, AlarmParameterSettings> getMap(LambdaQueryWrapper<AlarmParameterSettings> wrapper) {
        Map<Long, AlarmParameterSettings> dataMap=new HashMap<>();
        List<AlarmParameterSettings> list=new ArrayList<>();
        if(null!=wrapper){
            list=list(wrapper);
        }else {
            list=list();
        }
        if(null!=list&&list.size()>0) {
            dataMap = list.stream().collect(Collectors.toMap(AlarmParameterSettings::getId, Function.identity()));
        }
        return dataMap;
    }


    @Override
    public String saveParams(List<PointTableParamsDTO> ptParmas) {
        String gatewayDeviceId = null;
        if(null == ptParmas || ptParmas.isEmpty()){
            return gatewayDeviceId;
        }
        Map<Long,Integer> ptParmasMap = new HashMap<>();
        ptParmas.forEach(item->{
            ptParmasMap.put(item.getPointTableId(),item.getDisplay());
        });
        List<AlarmParameterSettings> settings = new ArrayList<>();
        //判重
//        repeatPointTableId(ptParmasMap.keySet());
        List<IotPointTable> pointTableList = iotPointTableService.listByIds(ptParmasMap.keySet());
        gatewayDeviceId = pointTableList.get(0).getGatewayDeviceId();

//        lambdaUpdate().notIn(AlarmParameterSettings::getPointTableId, ptParmasMap.keySet()).set(AlarmParameterSettings::getDisplay,0).update();
        lambdaUpdate().eq(AlarmParameterSettings::getGatewayDeviceId,gatewayDeviceId).remove();

        pointTableList.forEach(item->{
            AlarmParameterSettings setting = new AlarmParameterSettings();
            setting.setDeviceId(item.getDeviceId());
            setting.setGatewayDeviceId(item.getGatewayDeviceId());
            setting.setPointId(item.getPointId());
            setting.setPointName(item.getPointName());
            setting.setFieldName(item.getFieldName());
            setting.setParameterType(item.getDataType());
            setting.setDisplay(ptParmasMap.get(item.getId()));
            setting.setPointTableId(item.getId());
            settings.add(setting);
        });
        parameterSettingsMapper.batchInsert(settings);
        return gatewayDeviceId;
    }

    @Override
    public List<AlarmSettingPonitTablesDTO> settingPonitTableList(Set<Long> pointTableId,Long deviceId,String pointName) {
        // 子设备逻辑
//        List<MesDevice> children = mesDeviceService.lambdaQuery().eq(MesDevice::getParentId, deviceId).list();
//        List<Long> deviceIds = new ArrayList<>();
//        if(null!=children&& !children.isEmpty()){
//            deviceIds = children.stream().map(MesDevice::getId).collect(Collectors.toList());
//            return parameterSettingsMapper.settingPonitTables(pointTableId,deviceId,pointName,deviceIds);
//        }
        List<AlarmSettingPonitTablesDTO> pointTables = parameterSettingsMapper.settingPonitTables(pointTableId,deviceId,pointName,null);
        return pointTables;
    }

    /**
     * 点表是否已使用
     * @param pointTableId
     */
    private void   repeatPointTableId(Set<Long> pointTableId){
        StringBuilder  sb  = new StringBuilder();
        List<AlarmSettingPonitTablesDTO> pointTables = settingPonitTableList(pointTableId,null,null);
        pointTables.forEach(item->{
            sb.append(item.getParamName());
            sb.append(",");
        });
        if(StringUtils.isNotBlank(sb.toString())){
            throw new ServiceException(String.format("以下参数【%s】已添加，请检查后操作！",sb.toString().substring(0,sb.toString().lastIndexOf(","))));
        }
    }



}
